//  菜单显示异常修改tinymce/skins/ui/oxide/skin.min.css:96 .tox-silver-sink的z-index值
//  http://tinymce.ax-z.cn/   中文文档

layui.define(['jquery'],function (exports) {
    var $ = layui.$

    var modFile = layui.cache.modules['tinymce'];

    var modPath = modFile.substr(0, modFile.lastIndexOf('.'))

    var setter = layui.setter || {}

    var response = setter.response || {}

    var settings = {
        base_url: modPath
        // , images_upload_url: '/rest/doUpload'//图片上传接口
        , images_upload_url: server.upimg//图片上传接口
        , language: 'zh_CN'
        , response: {
            statusName: response.statusName || 'code'//返回状态字段
            , msgName: response.msgName || 'msg'//返回消息字段
            , dataName: response.dataName || 'data'//返回的数据
            , statusCode: response.statusCode || {
                ok: 0//数据正常
            }
        }
        , success: function (res, succFun, failFun) {//上传完成回调
            if (res[this.response.statusName] == this.response.statusCode.ok) {
                succFun(res.data.src);
            } else {
                failFun(res[this.response.msgName]);
            }
        }
    };

    var t = {};

    t.render = function (option) {

        var admin = layui.admin || {}

        option.base_url = option.base_url ? option.base_url : settings.base_url

        option.language = option.language ? option.language : settings.language

        option.selector = option.selector ? option.selector : option.elem

        option.quickbars_selection_toolbar = option.quickbars_selection_toolbar ? option.quickbars_selection_toolbar : 'cut copy | bold italic underline strikethrough searchreplace'
 
        option.plugins = option.plugins ? option.plugins : 'quickbars print preview searchreplace autolink fullscreen image axupimgs link media codesample table charmap hr advlist lists wordcount imagetools indent2em lineheight fullscreen preview code hr clearstyle';

        option.toolbar = option.toolbar ? option.toolbar : 'undo redo removeformat | formatselect fontselect fontsizeselect | forecolor backcolor bold italic underline strikethrough hr | indent2em alignleft aligncenter alignright alignjustify outdent indent lineheight | link bullist numlist image axupimgs table codesample code fullscreen preview searchreplace';

        option.resize = false;

        option.elementpath = false

        option.branding = false;
		
		option.fontsize_formats = '8px 10px 12px 14px 16px 18px 20px 24px 26px 28px 30px 32px 34px 36px 48px 56px 72px'
		
		option.font_formats = '微软雅黑=Microsoft YaHei,Helvetica Neue,PingFang SC,sans-serif;苹果苹方=PingFang SC,Microsoft YaHei,sans-serif;宋体=simsun,serif;仿宋体=FangSong,serif;黑体=SimHei,sans-serif;Arial=arial,helvetica,sans-serif;Arial Black=arial black,avant garde;Book Antiqua=book antiqua,palatino;'
		
        // option.invalid_elements = 'strong,em';
        // option.invalid_styles = 'color font-size margin padding background-color  max-width width';
		
		/* option.formats = {alignleft: { selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img', classes: 'left' },
        aligncenter: { selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img', classes: 'center' },
        alignright: { selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img', classes: 'right' },
        alignjustify: { selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img', classes: 'full' },
        bold: { inline: 'span', classes: 'bold' },
        italic: { inline: 'span', classes: 'italic' },
        underline: { inline: 'span', classes: 'underline', exact: true },
        strikethrough: { inline: 'del' },
        forecolor: { inline: 'span', classes: 'forecolor', styles: { color: '%value' } },
        hilitecolor: { inline: 'span', classes: 'hilitecolor', styles: { backgroundColor: '%value' } },
        custom_format: { block: 'h1', attributes: { title: 'Header' }, styles: { color: 'red' } }} */
	   
       /* option.paste_auto_cleanup_on_paste = true;
		
        option.paste_remove_styles = true;
		
        option.paste_remove_styles_if_webkit = true;
		
        option.paste_strip_class_attributes = true;

        option.contextmenu_never_use_native = true; */

        option.menubar = option.menubar ? option.menubar : 'file edit insert format table';

        option.images_upload_url = option.images_upload_url ? option.images_upload_url : settings.images_upload_url;

        option.images_upload_handler = function (blobInfo, succFun, failFun) {

            var formData = new FormData();

            formData.append('target', 'edit');

            formData.append('edit', blobInfo.blob());

            var ajaxOpt = {

                url: server.upimg,

                dataType: 'json',

                type: 'POST',

                data: formData,

                processData: false,

                contentType: false,

                success: function (res) {

                    settings.success(res, succFun, failFun)

                },
                error: function (res) {

                    failFun("网络错误：" + res.status);

                }
            };

            if (typeof admin.req == 'function') {

                admin.req(ajaxOpt);

            } else {

                $.ajax(ajaxOpt);

            }
        }

        option.menu = option.menu ? option.menu : {
            file: {title: '文件', items: 'newdocument | print preview fullscreen | wordcount'},
            edit: {title: '编辑', items: 'undo redo | cut copy paste pastetext selectall | searchreplace'},
            format: {
                title: '格式',
                items: 'bold italic underline strikethrough superscript subscript | formats | forecolor backcolor | removeformat'
            },
            table: {title: '表格', items: 'inserttable tableprops deletetable | cell row column'},
        };

        $.ajax({//获取插件
            url: option.base_url + '/tinymce.js',

            dataType: 'script',

            cache: true,

            async: false,
        });
		
        tinymce.init(option);

        t.tinymce = tinymce;

        return tinymce.activeEditor;
    };
    exports('tinymce', t);
});